﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;

namespace CommonLibrary.Other
{
    /// <summary>
    /// 多线程复制文件
    /// </summary>
    public class FileClassCopyFile
    {
        private string sourceFile;
        string sourceDir;
        string targetDir;
        private string timeDir;

        public FileClassCopyFile(string in_sourceFile, string in_sourceDir, string in_targetDir, string in_timeDir)
        {
            sourceFile = in_sourceFile;
            sourceDir = in_sourceDir;
            targetDir = in_targetDir;
            timeDir = in_timeDir;
        }

        public void CopyFile()
        {
            try
            {
                string targetFile = sourceFile.Replace(sourceDir, targetDir);
                FileInfo sourceFileInfo = new FileInfo(sourceFile);
                FileInfo targetFileInfo = new FileInfo(targetFile);


                #region 修改属性

                if (targetFileInfo.Exists)
                {
                    targetFileInfo.Attributes = FileAttributes.Normal;
                }

                #endregion

                #region 如果文件不存在，直接复制
                if (!File.Exists(targetFile))
                {
                    if (Directory.Exists(targetFileInfo.Directory.FullName) == false)
                    {
                        Directory.CreateDirectory(targetFileInfo.Directory.FullName);
                    }

                    File.Copy(sourceFile, targetFile, true);
                    return;
                }
                #endregion

                #region 比较两个文件的md5

                //if (MyDll.Crc32.GetFileCRC32(sourceFile) != MyDll.Crc32.GetFileCRC32(targetFile))

                if (ClSecurity.ClMD5File(sourceFile) != ClSecurity.ClMD5File(targetFile))
                {
                    #region 创建timeDir
                    if (!Directory.Exists((sourceFileInfo.DirectoryName + @"\").Replace(sourceDir, timeDir)))
                    {
                        Directory.CreateDirectory((sourceFileInfo.DirectoryName + @"\").Replace(sourceDir, timeDir));
                    }
                    #endregion

                    #region 备份targetDir原有的文件到timeDir

                    File.Copy(targetFile, sourceFile.Replace(sourceDir, timeDir), true);

                    #endregion

                    #region 备份sourceDir文件到targetDir

                    File.Copy(sourceFile, targetFile, true);

                    #endregion
                }

                #endregion
            }
            catch (Exception ex)
            {
                ex.Log();
            }
        }

        public static bool DeleteFile(string in_TempDir, string in_FromDir, string in_ToDir, string in_TimeDir, bool in_IncludeChild)
        {
            try
            {
                if (in_TempDir.ToUpper().Contains("8E65BD49-B060-4A97-A4B0-C3FC80F8F26F"))
                {
                    return true;
                }

                DirectoryInfo dir;
                dir = new DirectoryInfo(in_TempDir);


                #region Check存在，Source不存在
                if (!Directory.Exists((dir.FullName + @"\").Replace(in_ToDir, in_FromDir)))
                {
                    #region 复制到timeDir

                    if (!Directory.Exists(in_ToDir.Replace(in_ToDir, in_TimeDir)))
                    {
                        #region 创建timeDir

                        Directory.CreateDirectory(in_ToDir.Replace(in_ToDir, in_TimeDir));

                        #endregion
                    }

                    FileSystem.MoveDirectory(dir.FullName, (dir.FullName + @"\").Replace(in_ToDir, in_TimeDir));

                    return true;

                    #endregion
                }
                #endregion

                #region 每一个文件
                FileInfo[] filesInfo;
                filesInfo = dir.GetFiles();

                foreach (FileInfo f in filesInfo)
                {
                    try
                    {
                        if (File.Exists(f.FullName.Replace(in_ToDir, in_FromDir))) continue;

                        if (!Directory.Exists((f.DirectoryName + @"\").Replace(in_ToDir, in_TimeDir)))
                        {
                            #region 创建timeDir

                            Directory.CreateDirectory((f.DirectoryName + @"\").Replace(in_ToDir, in_TimeDir));

                            #endregion
                        }

                        #region 只在targetDir中存在，而在sourceDir中不存在的文件

                        f.MoveTo(f.FullName.Replace(in_ToDir, in_TimeDir));

                        #endregion
                    }
                    catch (Exception err)
                    {
                        err.Log();
                        continue;
                    }
                }
                #endregion
                

                if (in_IncludeChild == false) return true;//是否递归

                DirectoryInfo[] directorInfo;
                directorInfo = dir.GetDirectories();

                foreach (DirectoryInfo di in directorInfo)
                {
                    DeleteFile(di.FullName, in_FromDir, in_ToDir, in_TimeDir, in_IncludeChild);
                }

                return true;
            }
            catch (Exception err)
            {
                err.Log();
                return false;
            }
        }

    }
}
